//
//  WebBrowserController.m
//  Finder
//
//  Created by cdm on 11-3-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "WebBrowserController.h"

@implementation WebBrowserController

@synthesize webView;
@synthesize backButtonItem;
@synthesize forwardButtonItem;
@synthesize refreshButtonItem;
@synthesize closeButtonItem;
@synthesize loadingIndictorView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
    }
    return self;
}

- (void)dealloc
{
    [self.webView release];
    [self.backButtonItem release];
    [self.forwardButtonItem release];
    [self.refreshButtonItem release];
    [self.closeButtonItem release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}


- (void)viewDidUnload
{
    [super viewDidUnload];

    self.webView = nil;
    self.backButtonItem = nil;
    self.forwardButtonItem = nil;
    self.refreshButtonItem = nil;
    self.closeButtonItem = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Event

- (IBAction)closeButtonItemOnClick:(id)sender
{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

- (IBAction)backButtonItemOnClick:(id)sender
{
    [self.webView goBack];
}

- (void)forwardButtonItemOnClick:(id)sender
{
    [self.webView goForward];
}

- (void)refreshButtonItemOnClick:(id)sender
{
    [self.webView reload];
}

#pragma mark - Load web url

- (void)loadWithURL:(NSString *)url
{
    if (![self isViewLoaded]) {
        [self loadView];
    }
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}

#pragma mark - Web view delegate

- (void)webViewDidStartLoad:(UIWebView *)webView 
{
    self.loadingIndictorView.hidden = NO;
    
    if ([self.webView canGoBack]) {
        self.backButtonItem.enabled = YES;
    } else {
        self.backButtonItem.enabled = NO;
    }
    
    if ([self.webView canGoForward]) {
        self.forwardButtonItem.enabled = YES;
    } else {
        self.forwardButtonItem.enabled = NO;
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.loadingIndictorView.hidden = YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
}

@end
